package org.jeecgframework.core.util.clean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jeecg.business.entity.site.SiteEntity;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.bean.FetchListInfo;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.clean.bean.InfoParam;
import org.jeecgframework.core.util.http.HttpClientUtil;

/**
 * 
 * <p>
 * 标题: 微信抓取工具类
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-5 下午4:31:56
 */
public class CommonHtmlCleanUtil {

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取列表
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param href
	 * @param site
	 * @return
	 */
	public static FetchListInfo getListUrl(String href, SiteEntity site, String cookies) {
		return getListUrl4Proxy(href, site, cookies, null, null);
	}

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取列表
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param href
	 * @param site
	 * @return
	 */
	public static FetchListInfo getListUrl4Proxy(String href, SiteEntity site, String cookies, String ip,
			Integer port) {
		// if(!href.startsWith("http")) href = site.getGdz() + href;
		if (!href.startsWith("http"))
			href = StringUtil.getAbsoluteURL(site.getGdz(), href);
		FetchListInfo info = new FetchListInfo();
		TagNode node = null;
		try {
			HtmlCleaner cleaner = new HtmlCleaner();
			String html = null;
			/**
			 * 默认轮训抓取3次
			 */
			int trynum = 1;
			int maxtrynum = 3;
			while (trynum <= maxtrynum) {
				trynum++;
				if (StringUtil.isNotEmpty(ip)) {
					html = HttpClientUtil.getSourceFromUrl(href, site.getWyzfj(), cookies, ip, port);
				} else {
					html = HttpClientUtil.getSourceFromUrl(href, site.getWyzfj(), cookies);
				}
				if (StringUtil.isNotEmpty(html))
					break;
			}

			node = cleaner.clean(html);
			Object[] ns = node.evaluateXPath(site.getLbtq());

			List<String> hreflist = new ArrayList<String>();
			List<Date> timeList = new ArrayList<Date>();
			if (ns != null && ns.length > 0) {
				TagNode cnode = null;
				for (Object cobj : ns) {
					cnode = (TagNode) cobj;
					hreflist.add(cnode.getAttributeByName("href"));
				}
				info.setHrefList(hreflist);
			}

			/**
			 * 获取时间列表
			 */
			if ("2".equals(site.getSjhqfs())) { // 列表中获取
				/**
				 * 时间
				 */
				String time = null;
				String timeXpath = null;
				String timeReg = null;
				TagNode timenode = null;
				if (StringUtil.isNotEmpty(site.getZwsj())) {
					if (site.getZwsj().indexOf("@|@") > 0) {
						String[] array = site.getZwsj().split("@\\|@");
						timeXpath = array[0];
						timeReg = array[1];
						Object[] times = node.evaluateXPath(timeXpath);
						if (times != null && times.length > 0) {
							for (Object cobj : times) {
								timenode = (TagNode) cobj;
								time = timenode.getText().toString();
								Pattern p = Pattern.compile(timeReg);
								Matcher m = p.matcher(time);
								if (m.find())
									time = m.group();
								// System.out.println(time);
								Date eventdate = DataUtils.parseDate(time);
								timeList.add(eventdate);
							}
							info.setTimeList(timeList);
						}

					}
				}
			}

			/**
			 * 下一页
			 */
			if (StringUtil.isNotEmpty(site.getLbxyy())) {
				/**
				 * param
				 */
				if ("param".equals(site.getXyyhqfs())) {

				} else {
					/**
					 * 如果是xpath
					 */
					ns = node.evaluateXPath(site.getLbxyy());
					if (ns != null && ns.length > 0) {
						String nextPage = ((TagNode) ns[0]).getAttributeByName("href");
						if (!nextPage.toLowerCase().startsWith("http")) {
							nextPage = StringUtil.getAbsoluteURL(site.getGdz(), nextPage);
						}
						info.setNextPage(nextPage);
					}
				}
			}

			return info;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("获取列表失败" + href);
			return null;
		}
	}

	public static HtmlParseResult getInfo(String href, SiteEntity site, String keyword, String cookies,
			InfoParam infoparamss) {
		return getInfo4proxy(href, site, keyword, cookies, infoparamss, null, null);
	}

	public static HtmlParseResult getInfo4proxy(String href, SiteEntity site, String keyword, String cookies,
			InfoParam infoparamss, String ip, Integer port) {
		String gethref = href;
		// if(!href.toLowerCase().startsWith("http")) href = site.getGdz() +
		// href;
		if (!href.toLowerCase().startsWith("http"))
			href = StringUtil.getAbsoluteURL(site.getGdz(), href);
		HtmlParseResult result = new HtmlParseResult();
		TagNode node = null;
		TagNode headNode = null;
		result.setUrl(href);
		result.setCharset(site.getWyzfj());
		result.setType(site.getJclx());
		result.setLy(site.getZddz());
		result.setCjzd(site.getZddz());
		result.setSite(site.getMc());
		result.setKeyword(keyword);
		result.setPcfs(site.getPcfs());
		try {
			HtmlCleaner cleaner = new HtmlCleaner();
			String html = null;
			if (StringUtil.isNotEmpty(ip)) {
				html = HttpClientUtil.getSourceFromUrl(href, site.getNyzfj(), cookies, ip, port);
			} else {
				html = HttpClientUtil.getSourceFromUrl(href, site.getNyzfj(), cookies);
			}
			// System.out.println(html.length()+"----"+href);
			/**
			 * 判断是否有二次跳转
			 */
			if ("1".equals(site.getRctz()) && StringUtil.isNotEmpty(site.getRcljqb())) {
				int p = html.indexOf(site.getRcljqb());
				if (p >= 0) {
					String newhref = html.substring(p + site.getRcljqb().length(),
							html.indexOf(site.getRcljhb(), p + site.getRcljqb().length()));
					if (StringUtil.isNotEmpty(ip)) {
						html = HttpClientUtil.getSourceFromUrl(newhref, site.getNyzfj(), cookies, ip, port);
					} else {
						html = HttpClientUtil.getSourceFromUrl(newhref, site.getNyzfj(), cookies);
					}
					node = cleaner.clean(html);
				}
			}
			if (node == null)
				node = cleaner.clean(html);

			/**
			 * 判断编码是否相同
			 */
			headNode = (TagNode) node.evaluateXPath("//head")[0];
			/**
			 * 如果是智能编码重新获取编码
			 */
			if ("1".equals(site.getZnbm())) {
				String encode = HttpClientUtil.getEncode(cleaner, headNode);
				if (!site.getNyzfj().equalsIgnoreCase(encode) && encode != null) {
					if (StringUtil.isNotEmpty(ip)) {
						html = HttpClientUtil.getSourceFromUrl(href, encode, cookies, ip, port);
					} else {
						html = HttpClientUtil.getSourceFromUrl(href, encode, cookies);
					}
					node = cleaner.clean(html);
				}
			}
			/**
			 * 标题
			 */
			String title = null;
			if (StringUtil.isNotEmpty(site.getZwbt())) {
				title = HtmlCleanBaseUtil.getTagContent(node, site.getZwbt());
				if (StringUtil.isEmpty(title))
					title = HtmlCleanBaseUtil.getTitle(node);
			} else {
				title = HtmlCleanBaseUtil.getTitle(node);
			}

			title = StringUtil.removeHTMLLable(title);

			/**
			 * 屏蔽词
			 */
			if (StringUtil.isNotEmpty(site.getPbc())) {
				String[] pbcs = site.getPbc().split("\\|");
				for (String p : pbcs) {
					title = title.replace(p, "");
				}
			}

			if ("bdtb".equals(site.getId())) {// 如果是贴吧，去掉_以后的
				if (title.indexOf("_") >= 0) {
					title = title.substring(0, title.lastIndexOf("_"));
				}
			}

			result.setTitle(title);

			/**
			 * 正文
			 */
			String zwnrXpath = "//body"; // 设置支持多个xpath表达式，使用"|"来分割
			// if(StringUtil.isNotEmpty(site.getZwnr())) zwnrXpath =
			// site.getZwnr();

			if (StringUtil.isNotEmpty(site.getZwnr()))
				zwnrXpath = site.getZwnr();
			System.out.println(zwnrXpath);
			String[] zwnrArray = zwnrXpath.split("\\|");
			String content = null;
			for (String zwnr : zwnrArray) {
				content = HtmlCleanBaseUtil.getTagContent(node, zwnr);
				if (StringUtil.isNotEmpty(content))
					break;
			}
			// String content = HtmlCleanBaseUtil.getTagContent(node,
			// zwnrXpath);
			/**
			 * 如果不匹配规则就抓取全部
			 */
			// if(StringUtil.isEmpty(content) && !zwnrXpath.equals("//body"))
			// content = HtmlCleanBaseUtil.getTagContent(node, "//body");

			content = StringUtil.removeHTMLLableExe(content);
			result.setBodycontent(content);

			/**
			 * 作者
			 */
			// if(StringUtil.isNotEmpty(site.getZwzz())){
			// String author = HtmlCleanBaseUtil.getTagContent(node,
			// site.getZwzz());
			// if(StringUtil.isNotEmpty(author))
			// result.setZz(author.trim());
			// }
			//

			/**
			 * 时间
			 */
			if ("2".equals(site.getSjhqfs())) {
				FetchListInfo fetchListInfo = infoparamss.getListInfo();
				List<String> urllist = fetchListInfo.getHrefList();
				List<Date> timeList = fetchListInfo.getTimeList();
				if (urllist.indexOf(gethref) >= 0)
					result.setEventDate(timeList.get(urllist.indexOf(gethref)));
			} else {
				String time = null;
				String timeXpath = null;
				String timeReg = null;
				if (StringUtil.isNotEmpty(site.getZwsj())) {
					if (site.getZwsj().indexOf("@|@") > 0) {
						String[] array = site.getZwsj().split("@\\|@");
						timeXpath = array[0];
						timeReg = array[1];
						time = HtmlCleanBaseUtil.getTagContent(node, timeXpath);
						Pattern p = Pattern.compile(timeReg);
						Matcher m = p.matcher(time);
						while (m.find()) {
							time = m.group();
							if (StringUtil.isNotEmpty(time) && time.startsWith("20"))
								break;
						}
					} else {
						time = HtmlCleanBaseUtil.getTagContent(node, site.getZwsj());
					}
					if (StringUtil.isNotEmpty(time)) {
						Date eventdate = DataUtils.parseDate(time);
						result.setEventDate(eventdate);
					}
				}
			}

			/**
			 * 设置IP和Address
			 */
			// Map<String , String> ipaddress = IcpUtil.getIcpInfo4Seo(href);
			// if(ipaddress != null){
			//// System.out.println("地址："+ipaddress);
			// result.setIp(ipaddress.get("ip"));
			// result.setAddress(ipaddress.get("address"));
			// }

			/**
			 * 如果是微信,需要获取正确的链接
			 */
			if ("wx".equals(site.getJclx())) {
				String myhref = null;
				if (html.indexOf("var msg_link = \"") > 0) {
					int position = html.indexOf("var msg_link = \"") + "var msg_link = \"".length();
					myhref = html.substring(position, html.indexOf("\"", position));
					result.setUrl(myhref);
				}
			}

			return result;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("抓取失败：" + href);
			return null;
		}
	}

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取页面信息
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param href
	 * @param site
	 * @return
	 */
	public static HtmlParseResult getInfo(String href, SiteEntity site, String keyword, String cookies) {
		return getInfo(href, site, keyword, cookies, null);
	}

}
